package com.tzxs.pay.main.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import com.igexin.sdk.PushManager;
import com.tzxs.pay.main.gt.IntentService;
import com.tzxs.pay.main.print.utils.print.AidlUtil;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.util.ArrayList;
import java.util.List;

/**
 * app
 */
public class TzApplication extends Application {
    private Activity topActivity;
    private List<Activity> activities = new ArrayList<>();
    private static Context instance;
    @Override
    public void onCreate() {
        super.onCreate();
        instance=getApplicationContext();
        //二维码扫描
        ZXingLibrary.initDisplayOpinion(this);
        //初始化打印服务
        AidlUtil.getInstance().connectPrinterService(this);
        //注册推送服务
        PushManager.getInstance().registerPushIntentService(getApplicationContext(), IntentService.class);

        //在这里注册了Activity
        this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
                topActivity = activity;
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                removeActivity(activity);
            }
        });
    }

    public static Context getInstance() {
        return instance;
    }

    //获取到栈顶activity
    public Activity getActivity() {
        //这个要判断是否为空
        Activity activity = activities.size() == 0 ? null : activities.get(activities.size() - 1);
        /*if (activity != null)
            Log.d("TzApplication", "getActivity: 当前栈顶Activity===  " + activity.getLocalClassName());*/
        return activity;
    }

    /**
     * 这个去每个Activity的onCreate里面调用一次
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 这个去每个Activity的onDestroy里面调用一次
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        activities.remove(activity);
    }
}
